﻿Imports DotNetNuke
Imports System.Web.UI
Imports DotNetNuke.UI.Utilities
Imports WindowsLive
Imports DotNetNuke.Services.Exceptions.Exceptions
Imports DotNetNuke.Common
Imports DotNetNuke.Services.Localization


Partial Class DesktopModules_DnnLiveMessenger_DNNLive
    Inherits DotNetNuke.Entities.Modules.PortalModuleBase

#Region "Fields"
    Private wll As WindowsLiveLogin = New WindowsLiveLogin(True)
    Dim RawMSNControls As System.Collections.Generic.Dictionary(Of String, String)
#End Region

    Private Sub Page_OnInit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init

        RawMSNControls = New System.Collections.Generic.Dictionary(Of String, String)
        RawMSNControls.Add("Profile", "<msgr:profile cid='$user' editable='true'> </msgr:profile>")
        RawMSNControls.Add("PersonalMessage", "<msgr:personal-message   cid='$user'></msgr:personal-message>")
        RawMSNControls.Add("SignIn", "<msgr:sign-in></msgr:sign-in>")
        RawMSNControls.Add("PresenceStatus", "<msgr:status cid='$user' mode='both'></msgr:status>")
        RawMSNControls.Add("DisplayPicture", "<msgr:display-picture cid='$user' presence-enabled='true'></msgr:display-picture>")
        RawMSNControls.Add("DisplayName", "<msgr:display-name cid='$user'></msgr:display-name>")
        RawMSNControls.Add("ConversationList", "<msgr:conversation-list> </msgr:conversation-list>")
        RawMSNControls.Add("ContactPicker", "<msgr:contact-picker hide-offline-contacts='true' hide-unavailable-contacts='true' max-selection-count='50'> </msgr:contact-picker>")
        RawMSNControls.Add("ContactList", "<msgr:contact-list word-wheel-enabled='true' hide-offline-contacts='true'> </msgr:contact-list>")
        RawMSNControls.Add("LiveBar", "<msgr:bar></msgr:bar>")
        RawMSNControls.Add("LiveApp", "<script type='text/javascript'> jQuery(document).ready(function() { jQuery('html').attr('xmlns:msgr', 'http://messenger.live.com/2009/ui-tags');})</script><msgr:app id='appTag' application-verifier-token='" + ApplicationVerifier + "' privacy-url='" + Privacy + "' channel-url='" + Channel + "' token-url='" + RefreshToken + "' ></msgr:app>")


        For Each key As String In RawMSNControls.Keys
            LiveControlRadioButtonList.Items.Add(New System.Web.UI.WebControls.ListItem(LocalizedText(key), key))
        Next

        SaveButton.Text = LocalizedText("SaveButton")

    End Sub

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click        
        Dim objModules As New DotNetNuke.Entities.Modules.ModuleController
        objModules.UpdateTabModuleSetting(TabModuleId, "LiveControl", LiveControlRadioButtonList.SelectedValue)
        DotNetNuke.Entities.Modules.ModuleController.SynchronizeModule(Me.ModuleId)

        EmitLiveControl(LiveControlRadioButtonList.SelectedValue)        

    End Sub

    Protected Sub EmitLiveControl(ByVal ControlName As String)
        Me.LiveControlPanel.Controls.Clear()
        If (RawMSNControls.ContainsKey(ControlName)) Then
            Me.LiveControlPanel.Controls.Add(New LiteralControl(RawMSNControls(ControlName)))
        End If

    End Sub

#Region "Event Handlers"

    Public Function LocalizedText(ByVal Key As String) As String
        LocalizedText = Localization.GetString(Key, LocalResourceFile)
    End Function

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try


            'register Live Messenger script references
            Page.ClientScript.RegisterClientScriptInclude("LiveMessenger", "http://www.wlmessenger.net/api/3.0/loader.js")
            Page.ClientScript.RegisterClientScriptBlock(Me.Page.GetType(), "LiveLoader", "Microsoft.Live.Core.Loader.load(['messenger.ui', 'messenger.ui.styles.core']);", True)

            'by default turn this off for all actors
            OptionsPanel.Visible = False

            'grab the setting, if there is one
            Dim LiveControl As String = ""
            If (Settings("LiveControl") <> Nothing) Then
                LiveControl = CStr(Settings("LiveControl"))
            End If

            'if we are a super user and the "Mode" is Edit, show the options and select the correct radio option
            If (Me.UserInfo.IsSuperUser And IsEditable) Then
                OptionsPanel.Visible = True
                If Not (Page.IsPostBack) Then LiveControlRadioButtonList.SelectedValue = LiveControl
            End If

            If Not (Page.IsPostBack) Then
                'emit the live control
                EmitLiveControl(LiveControl)
            End If
        Catch exc As Exception
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

#End Region
#Region "Properties"

    Protected ReadOnly Property ApplicationVerifier() As String
        Get
            Return wll.GetAppVerifier()
        End Get
    End Property

    Protected ReadOnly Property Channel() As String
        Get
            Return Page.ClientScript.GetWebResourceUrl(wll.GetType(), "Channel.html")
        End Get
    End Property

    Protected ReadOnly Property Privacy() As String
        Get
            Return NavigateURL("Privacy")
        End Get
    End Property

    Protected ReadOnly Property RefreshToken() As String
        Get
            Return Me.ResolveUrl("~/RefreshMessengerToken.aspx")
        End Get
    End Property

#End Region
End Class
